package org.example.fronted.controller;

import org.example.entity.Data;
import org.example.entity.SmsCode;
import org.example.entity.User;
import org.example.fronted.service.RegisterService;
import org.example.fronted.service.SmsService;
import org.example.fronted.service.impl.SmsServiceImpl;
import org.example.fronted.utils.CheckCode;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RegisterController {

    private RegisterService registerService;
    private SmsService smsService;
    public RegisterController(RegisterService registerService,SmsService smsService){
        this.registerService=registerService;
        this.smsService=smsService;
    }

//    data:{username:username,password:password,email:email,phone:phone,sex:sex},
    //用户注册成功，保存用户信息

    @RequestMapping("/registerController")
    public Object saveUser(HttpServletRequest request) throws IOException {

        String username = request.getParameter("username");
        System.out.println(username);
        User user=new User();
        user.setUName(request.getParameter("username"));
        user.setUPassword(request.getParameter("password"));
        user.setUEmail(request.getParameter("email"));
        user.setUPhone(request.getParameter("phone"));
        user.setUSex(request.getParameter("sex"));

        boolean flag = registerService.saveUser(user);
        Data data=null;
        if (flag){
            data=new Data(1,"注册成功");
        }else {
            data=new Data(-1,"注册失败");
        }
        return data;
    }

    //判断验证码是否错误和过期
    @RequestMapping("/PhoneRegister")
    protected Object PhoneRegister(HttpServletRequest req,String phone,String validateCode) throws ServletException, IOException {
        System.out.println(phone);
        System.out.println(validateCode);

        HttpSession session = req.getSession();
        SmsCode smsCode = (SmsCode)session.getAttribute("smsCode");
        Data data = null;

        if(null == smsCode || !smsCode.getCode().equals(validateCode)) { //用户没有验证码
            data = new Data(-2, "验证码错误.");
            return data;
        }


        if(!smsCode.isExpire()) { //验证码过期了
            data = new Data(-2, "验证码过期.");
            return data;
        }

        // 验证正确，但是手机号不正确, 属于高危风控
        if(smsCode.getCode().equals(validateCode) && !smsCode.getPhone().equals(phone)) {
            data = new Data(-3, "手机号不正确.");
            // TODO 在实际工作中，要将原本的手机推送到风控系统

            return data;
        }
        else {
            data = new Data(1, "验证成功.");
            return data;
        }

    }

    //获取验证码和给手机发送验证码
    @RequestMapping("/getValidateCode")
    private String getValidateCode(HttpServletRequest request, String phone){


        System.out.println(phone);

//        获取验证码
        String code = CheckCode.createRandom(true, 4);
//        String code = "1234";
        Map m=new HashMap();
        m.put("code",code);

        //调用方法给用户手机发送验证码
        SmsService smsDao=new SmsServiceImpl();
        smsDao.send(phone,m);

        SmsCode smsCode = new SmsCode(code, phone, 120);
        request.getSession().setAttribute("smsCode",smsCode);
//        response.getWriter().write(JSON.toJSONString(code));

        return code;
    }

    @RequestMapping("/checkUser")
    protected Object checkUser(String username) throws ServletException, IOException {

        boolean isExists = registerService.checkUser(username);

        Data data = null;
        if(isExists) {  //存在
            data = new Data(-1, "用户名已存在，请重新输入用户名");
            return data;
        }else {
            data = new Data(1, "success");
            return data;
        }

    }

}
